/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.drest.service.youtube.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import be.roam.drest.service.youtube.YouTubeUserProfile;
import be.roam.util.StringUtil;

public class YouTubeUserProfileHandler extends YouTubeResponseHandler {
    
    private YouTubeUserProfile userProfile;
    
    public YouTubeUserProfile getUserProfile() {
        return userProfile;
    }

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (!isErrorResponse()) {
            if ("user_profile".equals(localName)) {
                userProfile = new YouTubeUserProfile();
            } else if ("first_name".equals(localName) || "last_name".equals(localName) || "about_me".equals(localName)
                    || "age".equals(localName) || "video_upload_count".equals(localName) || "video_watch_count".equals(localName)
                    || "homepage".equals(localName) || "hometown".equals(localName) || "gender".equals(localName)
                    || "occupations".equals(localName) || "companies".equals(localName) || "city".equals(localName)
                    || "country".equals(localName) || "books".equals(localName) || "hobbies".equals(localName)
                    || "movies".equals(localName) || "relationship".equals(localName) || "friend_count".equals(localName)
                    || "favourite_video_count".equals(localName) || "currently_on".equals(localName)) {
                characterBuffer = new StringBuilder();            
            } else {
                super.startElement(uri, localName, qName, attributes);
            }
        } else {
            super.startElement(uri, localName, qName, attributes);
        }
    }

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (!isErrorResponse()) {
            if ("first_name".equals(localName)) {
                userProfile.setFirstName(getCharacters(true));
            } else if ("last_name".equals(localName)) {
                userProfile.setLastName(getCharacters(true));
            } else if ("about_me".equals(localName)) {
                userProfile.setAbout(getCharacters(true));
            } else if ("age".equals(localName)) {
                userProfile.setAge(StringUtil.parseInt(getCharacters(true)));
            } else if ("video_upload_count".equals(localName)) {
                userProfile.setNrVideosUploaded(StringUtil.parseInt(getCharacters(true)));
            } else if ("video_watch_count".equals(localName)) {
                userProfile.setNrVideosWatched(StringUtil.parseInt(getCharacters(true)));
            } else if ("homepage".equals(localName)) {
                userProfile.setHomepage(getCharacters(true));
            } else if ("hometown".equals(localName)) {
                userProfile.setHometown(getCharacters(true));
            } else if ("gender".equals(localName)) {
                userProfile.setGender(getCharacters(true));
            } else if ("occupations".equals(localName)) {
                userProfile.setOccupations(getCharacters(true));
            } else if ("companies".equals(localName)) {
                userProfile.setCompanies(getCharacters(true));
            } else if ("city".equals(localName)) {
                userProfile.setCity(getCharacters(true));
            } else if ("country".equals(localName)) {
                userProfile.setCountry(getCharacters(true));
            } else if ("books".equals(localName)) {
                userProfile.setBooks(getCharacters(true));
            } else if ("hobbies".equals(localName)) {
                userProfile.setHobbies(getCharacters(true));
            } else if ("movies".equals(localName)) {
                userProfile.setMovies(getCharacters(true));
            } else if ("relationship".equals(localName)) {
                userProfile.setRelationship(getCharacters(true));
            } else if ("friend_count".equals(localName)) {
                userProfile.setNrFriends(StringUtil.parseInt(getCharacters(true)));
            } else if ("favourite_video_count".equals(localName)) {
                userProfile.setNrFavouriteVideos(StringUtil.parseInt(getCharacters(true)));
            } else if ("currently_on".equals(localName)) {
                userProfile.setCurrentlyOn(StringUtil.parseBoolean(getCharacters(true)));
            } else {
                super.endElement(uri, localName, qName);
            }
        } else {
            super.endElement(uri, localName, qName);
        }
    }
    
}
